package me.common.res
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	
	import me.module.alert.Alert;
	
	
	/**
	 * @author 缺硒
	 */
	public class GameLoader extends Loader
	{
		private var _url:String;
		
		/**
		 * callBack function 
		 */		
		private var _callFunc:Function;
		/**
		 * 自定义数据 
		 */		
		private var _args:Object;
		
		// 构造函数
		public function GameLoader(url:String = "", callFunc:Function = null,args:Object = null)
		{
			_url = url;
			_args = args;
			_callFunc = callFunc;
			
			if(url != "") loadElement(url,_callFunc);
		}
		
		public function loadElement(url:String, callFunc:Function,args:Object = null):void
		{
			_url = url;
			_args = args;
			_callFunc = callFunc;
			alpha = 0;
			unload();
			
			contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
			load(new URLRequest(url));
		}
		
		/**
		 * load complete
		 * @param evt 
		 */		
		private function onComplete(evt:Event):void
		{
			contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
			
			var bmd:BitmapData = (content as Bitmap).bitmapData;
			if(bmd == null) throw new Error("载入的图像不存在！！");
			
			if(_callFunc != null)
				_callFunc.apply(null,[bmd,_args]);
		}
		
		private function onError(evt:IOErrorEvent):void
		{
			Alert.showLog("** 加载失败: " + _url);
			trace("加载失败: ", evt.text)
		}
		//
	}
}